﻿using Azure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MyWeb.ServiceBus.Common;
using MyWeb.ServiceBus.Model.Dto;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services.Db;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.ServiceBus.Services.Impl
{
    public class ApplicationService : BaseEFService<Application>, IApplicationService
    {
        private readonly Context db;
        private readonly IOperatorProvider provider;
        public ApplicationService(Context context, IOperatorProvider provider) : base(context)
        {
            db = context;
            this.provider = provider;
        }

        public async Task<int> AddAsync(ApplicationDto dto)
        {
            var entity = new Application()
            {
                Id = Snowflake.NextId(),
                AppSystemId = dto.AppSystemId,
                Code = dto.Code,
                Name = dto.Name,
                Abbr = dto.Abbr,
                Simplify = dto.Simplify,
                Sign = dto.Sign,
                AppToken = dto.AppToken,
                CreateDate = DateTime.Now,
                CreateUserId = provider.GetUserInfo().Id,
            };
            db.Application.Add(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(long id)
        {
            var entity = await db.Application.Where(a => a.Id == id).FirstAsync();
            if(entity == null)
            {
                throw new ArgumentNullException("实体不存在！");
            }
            db.Remove(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<int> EditAsync(ApplicationDto dto)
        {
            var entity = await db.Application.Where(a => a.Id == dto.Id).FirstAsync();

            entity.AppSystemId = dto.AppSystemId;
            entity.Code = dto.Code;
            entity.Name = dto.Name;
            entity.Abbr = dto.Abbr;
            entity.Simplify = dto.Simplify;
            entity.Sign = dto.Sign;
            entity.AppToken = dto.AppToken;
            entity.UpdateDate = DateTime.Now;
            entity.UpdateUserId = provider.GetUserInfo().Id;

            db.Update(entity);
            return await db.SaveChangesAsync();
        }

        public async Task<List<ApplicationDto>> QueryPageAsync(ApplicationDto dto)
        {
            var expression = ExtLinq.True<ApplicationDto>();
            if (!string.IsNullOrEmpty(dto.Key))
            {
                expression = expression.And(o => o.Code.Contains(dto.Key) || o.Abbr.Contains(dto.Key));
            }
            var linq = db.Application
                .Select(a => new ApplicationDto
                {
                    Id = a.Id,
                    AppSystemId = a.AppSystemId,
                    Code = a.Code,
                    Name = a.Name,
                    Abbr = a.Abbr,
                    Simplify = a.Simplify,
                    Sign = a.Sign,
                    AppToken = a.AppToken,
                    CreateDate = a.CreateDate,
                    CreateUserId = a.CreateUserId,
                    UpdateDate = a.UpdateDate,
                    UpdateUserId = a.UpdateUserId,
                })
                .Where(expression);
            var result = new List<ApplicationDto>();
            if(dto.Pagination != null)
            {
                dto.Pagination.DataCount = linq.Count();
                result = await linq.Skip((dto.Pagination.Page - 1) * dto.Pagination.PageSize)
                .Take(dto.Pagination.PageSize)
                .ToListAsync();
            }
            else
            {
                result = await linq.ToListAsync();
            }
            return result;
        }
    }
}
